//Write by oshogbo//vx
//23.07.2011
//http://code.google.com/p/streamobs

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;

public class Window extends JFrame implements MouseListener, MouseMotionListener
{
  private final int width = 227, height = 170;
  private final int textStartX = 30;
  private final int textStartY = 46;
  private final int textNext = 18;
  
  private Font font = new Font("Arial", Font.BOLD, 14);
  private FontMetrics fontMetrices = getFontMetrics(font);
  private ArrayList<Channels> channel;
  private Image background = new ImageIcon("gfx/napiprojekt.jpg").getImage();
  private Image imgNoLive = new ImageIcon("gfx/statusbuttonoff.jpg").getImage();
  private Image imgLive = new ImageIcon("gfx/statusbuttonON.jpg").getImage();
  private int selectedItem = -1;

  Window(ArrayList<Channels> channel)
  {
    super("Stream Obs");
    setUndecorated(true);
    this.channel = channel;

    setIconImage(new ImageIcon("gfx/SCicosmall.jpg").getImage());

    setSize(width, height);
    setBackground(Color.BLACK); 

    addMouseListener(this);
    addMouseMotionListener(this);
  }

  public void setVisible(boolean b)
  {	
    if( b == true )
    {
      Rectangle maxBounds = 
	  GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
      setLocation(maxBounds.width - width, maxBounds.height - height);
    }

    super.setVisible(b);
  }

  public void paint(Graphics g)
  {

    g.drawImage(background, 0, 0, this);
    g.setFont(font);

    for(int i = 0; i < channel.size(); i++)
    {
      if(channel.get(i).isLive() == true)	
	g.drawImage(imgLive, 7, 32 + (i * textNext), this);
      else
	g.drawImage(imgNoLive, 7, 32 + (i * textNext), this);
      
      if(selectedItem == i)
        g.setColor(new Color(0xFF, 0x00, 0x00));
      else
        g.setColor(new Color(0xFF, 0xe5, 0x3f));
      
      g.drawString(channel.get(i).getNameToShow(), textStartX, textStartY + (i * textNext));
    }
  }
 
  private int mouseOnChannel(int x, int y)
  {
    int n = (y - textNext) / textNext - 1;
    if( n < 0 || n >= channel.size() )
      return -1;

    Channels channel = this.channel.get(n);
    
    if(textStartX <= x && x <= textStartX + fontMetrices.stringWidth(channel.getNameToShow()))
      return n;

    return -1;
  }

  public void mouseClicked(MouseEvent e)
  {
    //check if x on background was pressed
    if((e.getX() > 206) && (e.getX() < 223) 
	&& (e.getY() > 5) && (e.getY() < 24))
      setVisible(false);

    try
    {
      if(selectedItem != -1)
	Desktop.getDesktop().browse(new URI(channel.get(selectedItem).getAddres()));
    }catch(Exception ex)
    {
      System.out.println("Cannot open default browser");
    }
  }
  
  public void mouseMoved(MouseEvent e)
  {
    int last = selectedItem;
    selectedItem = mouseOnChannel(e.getX(), e.getY());
    if(last != selectedItem)
    {
      try
      {
	Thread.sleep(25);
      }catch(Exception ex)
      {

      }
      repaint();
    }
  }

  public void mouseDragged(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}

  public void mouseExited(MouseEvent e)
  {
    if(selectedItem != -1)
    {
      repaint();
      selectedItem = -1;
    }
  }

  public void mousePressed(MouseEvent e){}
  public void mouseReleased(MouseEvent e){}
}
